/**
*
* Copyright (c) 2011~2012 galaxy engine team.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
*  Abstract:
*    
*
*  Author:
*    freecnjet (freecnjet@gmail.com)
*
*  Revision History:
*    Created on: 2012-03-18
*
*/

#ifndef _REFLECT_H_
#define _REFLECT_H_


__BEGIN_NAMESPACE


struct Type {
	enum {
		TVOID = 0,
		TBOOL,
		TSBYTE,
		TBYTE,
		TSHORT,
		TWORD,
		TLONG,
		TDWORD,
		THYPER,
		TQWORD,
		TCHAR,
		TWCHAR,
		TFLOAT,
		TDOUBLE,
		TARRAY,
		TPOINTER,
		TREFERENCE,
    TREDEFINE,
		TENUMERATION,
		TINTERFACE,
		TCLASS,
		TOBJECT,
		TMAX
	};

     virtual const char* Name() = 0;
	 virtual int Category() = 0;
	 virtual unsigned int Length() = 0;
};

struct ArrayType : public Type {
	virtual bool IsFixedSize() = 0;
	virtual bool IsReadOnly() = 0;
	virtual unsigned int Rank() = 0;
	virtual unsigned int Bound(unsigned int rankid) = 0;
};

struct ReferenceType : public Type {
	virtual Type* ReferenceTo() = 0;
};

struct PointerType : public Type {
	virtual Type* PointerTo() = 0;
};

struct EnumInfo {
	virtual const char* Name() = 0;
	virtual int Value() = 0;
};

struct EnumType : public Type {
	virtual unsigned int Count() = 0;
	virtual const EnumInfo* Items() = 0;
};

struct ParameterInfo {
	virtual Type* ParameterType() = 0;
	virtual bool IsOptional() = 0;
	virtual const char* Name() = 0;
	virtual void* RawDefaultValue() = 0;
};

struct ParameterStack {
	virtual unsigned int Count() = 0;
	virtual Type* GetParameterType(unsigned int pid) = 0;
	virtual void* GetRawParameter(unsigned int pid) = 0;
	virtual void Push(Type* type, void* raw_value) = 0;
};

struct MethodInfo {
    virtual bool IsPublic() = 0;
	virtual bool IsProtected() = 0;
	virtual bool IsPrivate() = 0;
	virtual bool IsStatic() = 0;
	virtual bool IsConstructor() = 0;
	virtual bool IsVirtual() = 0;	
	virtual Type* ReturnType() = 0;
	virtual unsigned int ParameterCount() = 0;
	virtual ParameterInfo* Parameters(unsigned int pid) = 0;
	virtual ParameterStack* GetParameterStack() = 0;
	virtual int Invoke(void* obj ) = 0;
};

struct InterfaceType : public Type {
	virtual unsigned int BaseCount() = 0;
	virtual InterfaceType* Bases(unsigned int bid) = 0;
	virtual unsigned int MethodCount() = 0;
	virtual MethodInfo* Methods(unsigned int mid) = 0;
};

struct FieldInfo {
	virtual bool IsPublic() = 0;
	virtual bool IsProtected() = 0;
	virtual bool IsPrivate() = 0;
	virtual bool IsStatic() = 0;
	virtual bool CanRead() = 0;
	virtual bool CanWrite() = 0;
	virtual Type* FieldType() = 0;
	virtual const char* Name() = 0;
	virtual void* RawDefaultValue() = 0;
	virtual void SetValue(void* obj/*object owen this field*/, void* val ) = 0;
	virtual void* GetValue(void* obj) = 0;
};

struct ClassType : public InterfaceType {
	virtual unsigned int FieldCount() = 0;
	virtual FieldInfo* Fields(unsigned int fid) = 0;
};

typedef signed char sbyte;
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;
typedef long long hyper;
typedef unsigned long long qword;


struct Object {
	Object() {}
	virtual ~Object() {}
	virtual InterfaceType* GetClass() = 0;
};


/**
 * utility functions
 */
inline bool IsTypeEqual(Type* type0, Type* type1) {
	if( type0==type1 )
		return true;

	if( Type::TPOINTER==type0->Category() && Type::TPOINTER==type1->Category() )
		return IsTypeEqual(((PointerType*)type0)->PointerTo(), ((PointerType*)type1)->PointerTo());

	if( Type::TREFERENCE==type0->Category() && Type::TREFERENCE==type0->Category() )
		return IsTypeEqual(((ReferenceType*)type0)->ReferenceTo(), ((ReferenceType*)type1)->ReferenceTo());

	return false;
}

inline bool IsSubClassOf(InterfaceType* subclz, InterfaceType* parent) {
	if( subclz==parent )
		return true;

	unsigned int base_count = subclz->BaseCount(); 
	for( unsigned int cid=0; cid<base_count; ++cid ) {
		if( IsSubClassOf( subclz->Bases(cid), parent) )
			return true;
	}
	return false;
}

inline bool IsInstanceOfClass(Object* obj, InterfaceType* clz) {
	return IsSubClassOf(obj->GetClass(), clz);
}


__END_NAMESPACE

#endif
